package com.prs.crm.action.crm;

import java.io.File;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.crm.Feedback;
import com.prs.crm.domain.crm.FeedbackType;
import com.prs.crm.service.crm.FeedbackService;
import com.prs.crm.service.crm.FeedbackTypeService;

public class FeedbackAction extends BaseAction {
	private Feedback feedback;
	private FeedbackService feedbackService;
	private PaginationSupport pagination;
	private Integer[] ids;

	private File upload;
	private String uploadFileName;

	private FeedbackTypeService feedbackTypeService;
	private Collection<FeedbackType> feedbackTypes;

	@Action("editFeedback")
	public String editFeedback() {
		feedbackTypes = this.getFeedbackTypeService().getAllFeedbackType();
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "submitFeedback", results = {
			@Result(name = "success", type = "redirect", location = "editFeedback"),
			@Result(name = "error", type = "chain", location = "editFeedback") })
	public String submitFeedback() {
		if (feedback.getTitle() == null
				|| "".equals(feedback.getTitle().trim())) {
			addActionError("主题不能太为空");
			return ERROR;
		}
		if (feedback.getCustomer() == null) {
			addActionError("客户不能太为空");
			return ERROR;
		}
		if (feedback.getType() == null) {
			addActionError("发聩方式不能太为空");
			return ERROR;

		}
		if(feedback.getFeedbacker()==null||feedback.getFeedbacker().trim().equals("")){
			addActionError("反馈人不能太为空");
			return ERROR;
		}if(feedback.getFeedbackWay()==null||"".equals(feedback.getFeedbackWay().trim())){
			addActionError("答复方式不能太为空");
			return ERROR;
			
		}if(feedback.getReplyDate()==null){
			addActionError("答复时间不能太为空");
			return ERROR;
		}if(feedback.getReplyer()==null){
			addActionError("答复人不能太为空");
			return ERROR;
		}
		this.getFeedbackService().submitFeedback(feedback, upload,
				uploadFileName);
		this.setPrompt("客户反馈提交成功");
		return SUCCESS;
	}

	@Action("listFeedback")
	public String listFeedback() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Feedback.class);
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getFeedbackService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("viewFeedback")
	public String viewFeedback() {
		return SUCCESS;
	}

	public Feedback getFeedback() {
		return feedback;
	}

	public void setFeedback(Feedback feedback) {
		this.feedback = feedback;
	}

	public FeedbackService getFeedbackService() {
		return feedbackService;
	}

	public void setFeedbackService(FeedbackService feedbackService) {
		this.feedbackService = feedbackService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public void setFeedbackTypeService(FeedbackTypeService feedbackTypeService) {
		this.feedbackTypeService = feedbackTypeService;
	}

	public FeedbackTypeService getFeedbackTypeService() {
		return feedbackTypeService;
	}

	public void setFeedbackTypes(Collection<FeedbackType> feedbackTypes) {
		this.feedbackTypes = feedbackTypes;
	}

	public Collection<FeedbackType> getFeedbackTypes() {
		return feedbackTypes;
	}

}
